package com.sgb.kjwl.viewmodel.machine;

import androidx.databinding.ObservableField;

import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.model.entity.lease.MatchEntity;
import com.sgb.kjwl.model.entity.self.LeaseAskStateEntity;
import com.sgb.kjwl.repository.MachineRepertory;
import com.sgb.kjwl.view.ui.activity.WebViewX5HostActivity;
import com.sgb.kjwl.view.ui.fragment.machine.MyAskLeaseFragment;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.base.model.entity.H5ReEntity;
import com.swgk.core.base.model.entity.ListResponse;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class MyAskLeaseFragModel extends BaseViewModel {

    private MyAskLeaseFragment askLeaseFragment;
    private MachineRepertory repertory;
    public ObservableField<MatchEntity> matchEntity = new ObservableField<>(new MatchEntity());

    public MyAskLeaseFragModel(MyAskLeaseFragment askLeaseF, MachineRepertory repertory) {
        this.askLeaseFragment = askLeaseF;
        this.repertory = repertory;
    }

    public void getDataList(String categoryType, String classNo, String statusNo, int page) {
        Map map = new HashMap();
        map.put("current", page);
        map.put("limit", 10);
        //因外部传递的categoryType是
        map.put("type", categoryType);//所属模块 1机械， 2 设备，3车辆，4 房产，5土地，6工厂，7农机 空字符串 全部
        map.put("firstCategoryNo", classNo);//分类ID
        map.put("status", statusNo);//状态
        map.put("title", "");
        DialogHelper.showProgressDialog(askLeaseFragment.getActivity(), "数据加载中...").setCanceledOnTouchOutside(false);
        APIManager.getInstance().getLeaseAPI().getAskRentPersonalList(map).enqueue(new Callback<BaseEntity<ListResponse<List<LeaseAskStateEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse<List<LeaseAskStateEntity>>>> call, Response<BaseEntity<ListResponse<List<LeaseAskStateEntity>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }

                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    if (null != response.body().getData() && null != response.body().getData().getList()) {
                        askLeaseFragment.showList(response.body().getData().getList());
                    } else {
                        askLeaseFragment.showList(new ArrayList<>());
                    }
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<LeaseAskStateEntity>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                askLeaseFragment.showList(new ArrayList<>());
            }
        });
    }

    public void getH5Url(int type, String id) {

        String urlName = "";
        if (type == 0) {//机械
            urlName = "mechanicsoffer";
        } else if (type == 1) {//设备
            urlName = "equipmentoffer";
        } else if (type == 2) {//车辆
            urlName = "vehicleoffer";
        } else if (type == 3) {//房产
            urlName = "houseoffer";
        } else if (type == 4) {//土地
            urlName = "landOfferDetails";
        } else if (type == 5) {//工厂
            urlName = "factoryOfferDetails";
        } else if (type == 6) {//农机
            urlName = "farmOfferDetails";
        }
        DialogHelper.showProgressDialog(askLeaseFragment.getActivity(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getPickAPI().getUrlSetting(urlName).enqueue(new Callback<BaseEntity<H5ReEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<H5ReEntity>> call, Response<BaseEntity<H5ReEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }

                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    String url = response.body().getData().getUrl() + "?id=" + id;
//                    String url = "http://192.168.2.103:8068/personCenterMechanicalDetail" + "?id=" + id;
                    WebViewX5HostActivity.start(askLeaseFragment.getActivity(), url);
                } else {
                    MToast.showToast(askLeaseFragment.getContext(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<H5ReEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }
}
